<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/10/17
 * Time: 15:41
 */

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');
Yaf_Loader::import(EXTEND_PATH.'/qmessage/Messages.php');

class LoginController extends Controller
{
    public function init()
    {
        $this->authentication = $this->authentication();
        $this->user = new UserModel();
        $this->session = Yaf_Session::getInstance();
    }

    public function indexAction()
    {
        if($this->authentication)
        {
            $this->redirect('/Home/index');
            exit;
        }
    }


    public function codeAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'phone' => 'require|number|length:11',
            ]);
            $validate->message([
                'phone.require' => '手机号不能为空',
                'phone.number' => '手机号只能是数字',
                'phone.length' => '手机号长度错误',
            ]);
            if ($validate->check($content)) {
                if($this->verifyCode($content['phone'])){
                    $code = 200;
                    $data['message'] = '验证码已下发,请注意查收';
                }else{
                    $code = 400;
                    $data['message'] = '验证码下发失败,请稍后重试';
                }
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function signInAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'phone' => 'require|number|length:11',
                'code' => 'require|number|length:4',
                'firm' => 'require|alphaNum|length:6|exist:firm,code',
            ]);
            $validate->message([
                'phone.require' => '手机号不能为空',
                'phone.number' => '手机号只能是数字',
                'phone.length' => '手机号长度错误',
                'code.require' => '验证码不能为空',
                'code.number' => '验证码只能是数字',
                'code.length' => '验证码长度错误',
                'firm.require' => '企业码不能为空',
                'firm.alphaNum' => '企业码只能是字母和数字',
                'firm.length' => '企业码长度错误',
                'firm.exist' => '企业码不存在',
            ]);
            $redis = Yaf_Registry::get('redis');
            $vCode = json_decode($redis->hget('Code',$content['phone']),true);
            if ($validate->check($content) && $vCode['code'] == $content['code']){
                $ip = $this->ip(1,true);
                $data = $this->user->signInAction($content,$ip);
                if($data['data']['status']){
                    $code = 200;
                    $redis->hdel('Code',$content['phone']);
                    $message = new Messages();
                    $message->account_import([(string)$data['data']['id'],$data['data']['nickname'],$data['data']['avatar']]);
                    $data['tim'] = $message->usrsig($data['data']['id']);
                    $data['token'] = $this->tokenProduce($data['data']['phone'],$data);
                    $this->session->token = $data['token'];
                    $data['udid'] = $data['data']['id'];
                    unset($data['data']['id']);
                }elseif($data['code']){
                    $code = 400;
                    unset($data['code']);
                }else{
                    $code = 400;
                    $data['message'] = '账户已被禁用';
                }
            }elseif($vCode['code'] != $content['code']){
                $code = 400;
                $data['message'] = '验证码错误';
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function signOutAction()
    {
        $redis = Yaf_Registry::get('redis');
        $out = $redis->hdel('User',$this->authentication['data']['phone']);
        if($out){
            $this->session->del('token');
        }
        $this->redirect('/');
    }
}